/*
 * SSLsplit - transparent and scalable SSL/TLS interception
 * Copyright (c) 2009-2014, Daniel Roethlisberger <daniel@roe.ch>
 * All rights reserved.
 * http://www.roe.ch/SSLsplit
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "thrqueue.h"
#include <string.h>
#include <stdio.h>


#ifndef LOGE
#define LOGE(fmt,...) fprintf(stderr, fmt, __VA_ARGS__)
#endif
#ifdef __linux
	#include <pthread.h>
	#include <semaphore.h>
	/*
	int sem_init(sem_t *sem, int pshared, unsigned int value);
	int sem_post(sem_t *sem);
	int sem_wait(sem_t *sem);
	int sem_trywait(sem_t *sem);
	int sem_destroy(sem_t *sem);

	*/
#elif defined(_WIN32)
	#include <windows.h>
	
	#define pthread_mutex_t CRITICAL_SECTION
	static int pthread_mutex_init(void *a, void *b)
	{
		InitializeCriticalSection((LPCRITICAL_SECTION)a);
		return 0;
	}

	#define pthread_mutex_destroy(a)    DeleteCriticalSection(a)
	#define pthread_mutex_lock(a)       EnterCriticalSection(a)
	#define pthread_mutex_unlock(a)     LeaveCriticalSection(a)

	#define sem_t HANDLE
	
	/*
	HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName);
	BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount);
	BOOL WINAPI CloseHandle( _In_ HANDLE hObject);
	DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds);
	*/
	int sem_init(sem_t *sem, int pshared, unsigned int value){
		if((*sem = CreateSemaphore(NULL, value, 0x7fffffff, NULL)) == NULL){
			return GetLastError();
		}
		return 0;
	}
	int sem_post(sem_t *sem){
		if(!ReleaseSemaphore(*sem, 1, NULL)){
			return GetLastError();
		}
		return 0;
	}

	int sem_wait(sem_t *sem){
		return WaitForSingleObject(*sem, INFINITE);
	}
	int sem_trywait(sem_t *sem){
		return WaitForSingleObject(*sem, 0);
	}

	int sem_destroy(sem_t *sem){
		return !CloseHandle(*sem);
	}

	#define sched_yield()               SwitchToThread()

#endif

struct thrqueue {
    void **data;
    size_t sz, n;
    size_t in, out;
    unsigned int block_enqueue : 1;
    unsigned int block_dequeue : 1;

	sem_t sem_space;
	sem_t sem_data;
	pthread_mutex_t mutex;
};



/*
 * Threadsafe, bounded-size queue based on pthreads mutex and conds.
 * Both enqueue and dequeue are available in a blocking and non-blocking
 * version.
 */


/*
 * Create a new thread-safe queue of size sz.
 */
thrqueue_t *
thrqueue_new(size_t sz)
{
	thrqueue_t *queue;

	if (!(queue = (thrqueue_t *)malloc(sizeof(thrqueue_t))))
		goto out0;
	if (!(queue->data = (void **)malloc(sz * sizeof(void*))))
		goto out1;
	if (0 != sem_init(&queue->sem_space, 0, sz))
		goto out2;
	if (0 != sem_init(&queue->sem_data, 0, 0))
		goto out3;
	if(0!= pthread_mutex_init(&queue->mutex, NULL)){
		goto out4;
	}

	queue->sz = sz;
	queue->n = 0;
	queue->in = 0;
	queue->out = 0;
	queue->block_enqueue = 1;
	queue->block_dequeue = 1;
	return queue;

out4:
	sem_destroy(&queue->sem_data);
out3:
	sem_destroy(&queue->sem_space);
out2:
	free(queue->data);
out1:
	free(queue);
out0:
	return NULL;
}

/*
 * Free all resources associated with queue.
 * The caller must ensure that there are no threads still
 * using the queue when it is free'd.
 */
void
thrqueue_free(thrqueue_t *queue)
{
	free(queue->data);
	pthread_mutex_destroy(&queue->mutex);
	sem_destroy(&queue->sem_space);
	sem_destroy(&queue->sem_data);
	free(queue);
}

/*
 * Enqueue an item into the queue.  Will block if the queue is full.
 * If enqueue has been switched to non-blocking mode, never blocks
 * but instead returns NULL if queue is full.
 * Returns enqueued item on success.
 */
void *
thrqueue_enqueue(thrqueue_t *queue, void *item)
{
	if(sem_wait(&queue->sem_space)){
		LOGE("%s:%m", __func__);
		return NULL;
	}
	pthread_mutex_lock(&queue->mutex);
	queue->data[queue->in++] = item;
	queue->in %= queue->sz;
	queue->n++;
	pthread_mutex_unlock(&queue->mutex);
	sem_post(&queue->sem_data);
	return item;
}

/*
 * Non-blocking enqueue.  Never blocks.
 * Returns NULL if the queue is full.
 * Returns the enqueued item on success.
 */
void *
thrqueue_enqueue_nb(thrqueue_t *queue, void *item)
{
	if(sem_trywait(&queue->sem_space)){
		return NULL;
	}
	pthread_mutex_lock(&queue->mutex);
	queue->data[queue->in++] = item;
	queue->in %= queue->sz;
	queue->n++;
	pthread_mutex_unlock(&queue->mutex);
	sem_post(&queue->sem_data);

	return item;
}

/*
 * Dequeue an item from the queue.  Will block if the queue is empty.
 * If dequeue has been switched to non-blocking mode, never blocks
 * but instead returns NULL if queue is empty.
 * Returns dequeued item on success.
 */
void *
thrqueue_dequeue(thrqueue_t *queue)
{
	void *item;
	if(sem_wait(&queue->sem_data)){
		LOGE("%s:%m", __func__);
		return NULL;
	}
	pthread_mutex_lock(&queue->mutex);
	item = queue->data[queue->out++];
	queue->out %= queue->sz;
	queue->n--;
	pthread_mutex_unlock(&queue->mutex);
	sem_post(&queue->sem_space);
	return item;
}

/*
 * Non-blocking dequeue.  Never blocks.
 * Returns NULL if the queue is empty.
 * Returns the dequeued item on success.
 */
void *
thrqueue_dequeue_nb(thrqueue_t *queue)
{
	void *item;
	if(sem_trywait(&queue->sem_data)){
		return NULL;
	}
	pthread_mutex_lock(&queue->mutex);
	if (queue->n == 0) {
		LOGE("%s:n==0 !!!!!!!!  this should not happen", __func__);
		pthread_mutex_unlock(&queue->mutex);
		return NULL;
	}
	item = queue->data[queue->out++];
	queue->out %= queue->sz;
	queue->n--;
	pthread_mutex_unlock(&queue->mutex);
	sem_post(&queue->sem_space);
	return item;
}

/*
 * Permanently make all enqueue operations on queue non-blocking and wake
 * up all threads currently waiting for the queue to become not full.
 * This is to allow threads to finish their work on the queue on application
 * shutdown, but not be blocked forever.
 */
void
thrqueue_unblock_enqueue(thrqueue_t *queue)
{
	queue->block_enqueue = 0;
	sem_post(&queue->sem_space);
	sched_yield();
}

/*
 * Permanently make all dequeue operations on queue non-blocking and wake
 * up all threads currently waiting for the queue to become not empty.
 * This is to allow threads to finish their work on the queue on application
 * shutdown, but not be blocked forever.
 */
void
thrqueue_unblock_dequeue(thrqueue_t *queue)
{
	queue->block_dequeue = 0;
	sem_post(&queue->sem_data);
	sched_yield();
}

/* vim: set noet ft=c: */
